package br.com.sovi.gwt.client.pesquisa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.sovi.gwt.client.entity.pesquisa.Opcao;
import br.com.sovi.gwt.client.entity.pesquisa.OpcaoBuilder;
import br.com.sovi.gwt.client.entity.pesquisa.Pesquisa;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Node;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;

public class PesquisaMenuPanel extends TreePanel {

	private SelectionListener selectionListener;

	public PesquisaMenuPanel(SelectionListener selectionListener) {
		this.selectionListener = selectionListener;
		
		this.setTitle("Pesquisas Municipais");
		this.setHeight("100%");
		this.setWidth(200);
		this.setCollapsed(true);
		this.setAnimate(true);
		this.setEnableDD(true);
		this.setContainerScroll(true);
		this.setAutoScroll(true);
		this.setRootVisible(false);
		this.setFrame(true);

		this.expand();

		populateTree();
	}

	private void populateTree() {
		TreeNode rootNode = new TreeNode();
		rootNode.appendChild(buildNode(buildDenguePesquisa()));

		this.setRootNode(rootNode);
	}

	private Node buildNode(final Pesquisa pesquisa) {
		TreeNode node = new TreeNode(pesquisa.getNome());
		
		node.addListener(new TreeNodeListenerAdapter() {
			@Override
			public void onClick(Node node, EventObject e) {
				selectionListener.onClick(pesquisa);
			}
		});
		
		return node;
	}

	private Pesquisa buildDenguePesquisa() {
		List<Opcao> opcoes = new ArrayList<Opcao>();

		opcoes.add(new OpcaoBuilder("Nível Alto", "/images/marker_red.png")
				.addPoint(-23.434427,-51.942372)
				.addPoint(-23.43927,-51.946321)
				.addPoint(-23.423007,-51.946406)
				.addPoint(-23.423165,-51.933789)
				.toOpcao());

		opcoes.add(new OpcaoBuilder("Nível Médio", "/images/marker_yellow.png")
				.addPoint(-23.40694,-51.958466)
				.addPoint(-23.394041,-51.95559)
				.addPoint(-23.442144,-51.9625)
				.addPoint(-23.420408,-51.973486)
				.addPoint(-23.430213,-51.907761)
				.addPoint(-23.432261,-51.876261)
				.addPoint(-23.424346,-51.881518)
				.toOpcao());

		opcoes.add(new OpcaoBuilder("Nível Baixo", "/images/marker_green.png")
				.addPoint(-23.400126,-51.946235)
				.addPoint(-23.450727,-51.914005)
				.addPoint(-23.434348,-51.973271)
				.addPoint(-23.417159,-51.929305)
				.addPoint(-23.43232,-51.893985)
				.addPoint(-23.416628,-51.890252)
				.addPoint(-23.411823,-51.90083)
				.toOpcao());

		return new Pesquisa("Controle da Dengue", new Date(), opcoes);
	}

	public void setSelectionListener(SelectionListener selectionListener) {
		this.selectionListener = selectionListener;
	}

	public SelectionListener getSelectionListener() {
		return selectionListener;
	}

	public static interface SelectionListener {
		void onClick(Pesquisa pesquisa);
	}
}
